﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SISCON.Dao;
using SISCON.Common;
using log4net;
using SISCON.Vo;
using SISCON.Model;

namespace SISCON.Bl
{
    
    /// <summary>
    /*
   Clase la cual verifica que el usuario actual pueda 
   hacer operaciones en los departamentos y manda a llamar al Dao correspondiente
   */
    /// </summary>
    public class DepartmentBL
    {

        private static ILog _Logger = LogManager.GetLogger(typeof(DepartmentBL));
        private DepartmentDAO _DepartmentDAO = new DepartmentDAO();
        private UserBL _UserBL = new UserBL();
        
        /// <summary>
        /// Verifica que el usuario actual pueda obtener todos los departamentos
        /// </summary>
        /// <param name="sessionUUID">Session del usuario</param>
        /// <returns>lista con todos los departamentos</returns>
        public List<Department> GetAllDepartments(string sessionUUID)
        {
            try
            {
                _UserBL.RequireRole(sessionUUID, Role.READ_ACCESS, Role.DEPARTMENTS);
                return _DepartmentDAO.GetAllDepartments();
            }
            catch (Exception ex)
            {
                if (!(ex is SISCONException))
                {
                    _Logger.Error("Error in GetAllDepartments()", ex);
                }
                throw ex;
            }
        }
        
        /// <summary>
        /// Verifica que el usuario pueda obtener un departamento en particular
        /// </summary>
        /// <param name="sessionUUID">Session del usuario</param>
        /// <param name="departmentId">Id del departamento</param>
        /// <returns>Un departamento</returns>
        public Department GetDepartment(string sessionUUID, string departmentId)
        {
            try
            {
                _UserBL.RequireRole(sessionUUID, Role.READ_ACCESS, Role.DEPARTMENTS);
                return _DepartmentDAO.GetDepartment(departmentId);
            }
            catch (Exception ex)
            {
                if (!(ex is SISCONException))
                {
                    _Logger.Error("Error in GetDepartment()", ex);
                }
                throw ex;
            }
        }
        
        /// <summary>
        /// verifica si el usuario puede crear o actualizar un departamento
        /// </summary>
        /// <param name="sessionUUID">Session del usuario</param>
        /// <param name="modifiedDepartmentId">Id modificado del departamento</param>
        /// <param name="departmentCode">Codigo del departamento</param>
        /// <param name="departmentName">Nombre del departamento</param>
        public void SaveDepartment(string sessionUUID, string modifiedDepartmentId, string departmentCode, string departmentName)
        {
            try
            {
                _UserBL.RequireRole(sessionUUID, Role.WRITE_ACCESS, Role.DEPARTMENTS);
                _DepartmentDAO.SaveDepartment(modifiedDepartmentId, departmentCode, departmentName);
            }
            catch (Exception ex)
            {
                if (!(ex is SISCONException))
                {
                    _Logger.Error("Error in SaveDepartment()", ex);
                }
                throw ex;
            }
        }
        
        /// <summary>
        /// Verifica si el usuario puede borrar un departamento
        /// </summary>
        /// <param name="sessionUUID">Session del usuario</param>
        /// <param name="departmentId">Id del departamento</param>
        public void DeleteDepartment(string sessionUUID, string departmentId)
        {
            try
            {
                _UserBL.RequireRole(sessionUUID, Role.WRITE_ACCESS, Role.DEPARTMENTS);
                _DepartmentDAO.DeleteDepartment(departmentId);
            }
            catch (Exception ex)
            {
                if (!(ex is SISCONException))
                {
                    _Logger.Error("Error in DeleteDepartment()", ex);
                }
                throw ex;
            }
        }

    }
}